//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shp.Runtime.Sitka.ServiceGateway.CloudDB;
using Shp.Runtime.Sitka.ServiceAgent;

namespace Shp.Runtime.DataModel.Sitka.Tests
{
    internal class MetadataInitialize
    {
        public string TenantAlias { get; set; }
        public Guid TenantId { get; set; }
        public string Conteiner { get; set; }

        public MetadataInitialize()
        {
            TenantId = Guid.NewGuid();
            TenantAlias = Guid.NewGuid().ToString().Replace('-', 'X');
        }

        private void AddType(string name, string id)
        {
            Entity entity = new Entity();
            entity.Id = id.ToString();
            entity.Kind = "Type";
            entity.Properties = new List<Property>();
            entity.Properties.Add(SitkaUtils.CreateProperty("Kind", "Type"));
            entity.Properties.Add(SitkaUtils.CreateProperty("Name", name));
            InsertEntity(entity);
        }

        public void CreateTypes()
        {
            AddType("Boolean (Yes | No)", "a82c6a4f-0389-4882-bd23-1ccf13a3c63e");
            AddType("Long text (up to 4000 characters)", "b44a04ca-3426-4c8d-9fc0-431de22d6f4e");
            AddType("Short text (up to 255 characters)", "fa564e7c-f59b-480f-a768-a1d02a34e32c");
            AddType("Number", "8863152f-f10e-4f7d-819f-b4bf05eb8b24");
            AddType("Date and time", "018d42ba-ca23-4e15-9c01-e941c86dd36f");
        }

        public void CreateEntityDef()
        {
            Entity entity = new Entity();
            entity.Id = "750438de-31a2-4d87-b0c6-28563840947c";
            entity.Kind = "EntityDef";
            entity.Properties = new List<Property>();
            entity.Properties.Add(SitkaUtils.CreateProperty("Name", "position"));
            entity.Properties.Add(SitkaUtils.CreateProperty("Kind", "EntityDef"));

            InsertEntity(entity);   

        }

        private void AddEntityDefField(string name, string typeId, string id)
        {
            Entity entity = new Entity();
            entity.Id = Guid.NewGuid().ToString();
            entity.Kind = "EntityDefField";
            entity.Properties = new List<Property>();
            entity.Properties.Add(SitkaUtils.CreateProperty("Name", name));
            entity.Properties.Add(SitkaUtils.CreateProperty("Kind", "EntityDefField"));
            entity.Properties.Add(SitkaUtils.CreateProperty("TypeId", typeId));
            entity.Properties.Add(SitkaUtils.CreateProperty("IsShared", true));
            entity.Properties.Add(SitkaUtils.CreateProperty("EntityDefId", "750438de-31a2-4d87-b0c6-28563840947c"));
            entity.Properties.Add(SitkaUtils.CreateProperty("TenantId", TenantId.ToString()));

            InsertEntity(entity);
        }

        public void CreateEntityDefFields()
        {
            AddEntityDefField("postedOn", "018d42ba-ca23-4e15-9c01-e941c86dd36f", "1b13bc12-86cf-483e-8501-17b191d4c2ef");
            AddEntityDefField("code", "fa564e7c-f59b-480f-a768-a1d02a34e32c", "31ec638a-d4b3-4211-b10a-aaae20e9cbf0");
            AddEntityDefField("title", "fa564e7c-f59b-480f-a768-a1d02a34e32c", "e24115b0-3ac7-44a5-a961-d0d349cb7184");
        }

        public void CreateTenant()
        {
            //create tenant
            Entity entity = new Entity();
            entity.Id = TenantId.ToString();
            entity.Kind = "tenant";
            entity.Properties = new List<Property>();
            entity.Properties.Add(SitkaUtils.CreateProperty("Kind", "tenant"));
            entity.Properties.Add(SitkaUtils.CreateProperty("Name", "name"));
            entity.Properties.Add(SitkaUtils.CreateProperty("Alias", TenantAlias));
            entity.Properties.Add(SitkaUtils.CreateProperty("SilverlightAccountId", Guid.NewGuid().ToString()));
            entity.Properties.Add(SitkaUtils.CreateProperty("SilverlightAccountKey", Guid.NewGuid().ToString()));
            entity.Properties.Add(SitkaUtils.CreateProperty("Style", Guid.NewGuid().ToString()));

            InsertEntity(entity);            
        }

        private void InsertEntity(Entity entity)
        {
            Scope scope = new Scope();
            scope.AuthorityId = "StratusAuthority";
            scope.ContainerId = Conteiner;

            StratusSoapServiceClient stratusProxy = new StratusSoapServiceClient();
            stratusProxy.Create(scope, entity);                        
        }
    }
}
